package study.nzy.com.studydemo;

import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.widget.TextView;

import com.nbpt.smarthomelibrary.SmartHub;

import java.text.DecimalFormat;

public class RoomActivity extends Activity {

    TextView tv_temp,tv_ill_temp;
    SmartHub smartHub;
    Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            //房间温度
            if(msg.what == 1){
                tv_temp.setText(String.valueOf(msg.obj));
            }else if(msg.what == 2){//灯温度
                tv_ill_temp.setText(String.valueOf(msg.obj));
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_room);
        tv_temp = findViewById(R.id.tv_temp);
        tv_ill_temp = findViewById(R.id.tv_ill_temp);
        smartHub = new SmartHub();
        new Thread(new Runnable() {
            @Override
            public void run() {
                float temperature = smartHub.getRoomTemperature();
                DecimalFormat df = new DecimalFormat("#.1");
                String format = df.format(temperature);
                Message message = new Message();
                message.obj = format;
                message.what = 1;
                handler.sendMessage(message);

                float roomIllumination = smartHub.getRoomIllumination();
                Message message1 = new Message();
                message1.obj = df.format(roomIllumination);
                message1.what =2;
                handler.sendMessage(message1);
            }
        }).start();
    }

}
